package com.javaee.my.user;

import com.javaee.interceptor.entity.user;
import com.javaee.interceptor.entity.userVo;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//注册servlet
@WebServlet("/register")
public class registerController extends HttpServlet implements BaseControllerApi {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");//获取用户名
        String password = req.getParameter("password");//获取密码
        user uservo = new user(0,username,password);//将用户名和密码封装
        //先查询数据库中是否有这样的数据
        Integer res = sqlsession.selectOne("com.javaee.my.dao.userMapper.checkUserName",
                username);//调用数据库查询
        //如果该查询到的数据不为空则表示已经注册
        if(StringUtils.isNotBlank(String.valueOf(res)))
        {
            req.getRequestDispatcher("index.jsp").forward(req,resp);//跳转到登录页面
            return;
        }else//否则执行插入操作
        {
            sqlsession.insert("com.javaee.my.dao.userMapper.register",uservo);
            sqlsession.commit();
            System.out.println("打印用户的编号"+uservo.getId());//控制台打印用户编号
            req.getRequestDispatcher("index.jsp").forward(req,resp);//跳转登录页面
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
